package CSDN;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 王圆豪
 * Date: 2025-05-13
 * Time: 12:55
 */
public class Test22 {
    public static void main(String[] args) {
        BlockingQueue<Integer> bq = new LinkedBlockingQueue<>();
        //生产者
        Thread t1 = new Thread(()->{
            int n = 0;
            while (true) {
                System.out.println("生产数据：" + n);
                try {
                    bq.put(n);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                n++;
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
            }
        });
        //消费者
        Thread t2 = new Thread(()->{
            while (true) {
                try {
                    System.out.println("消耗数据：" + bq.take());
                    Thread.sleep(1000);
                } catch (InterruptedException e) {

                }
            }
        });

        t1.start();
        t2.start();
    }
}
